package com.gitee.webdav.xml;

import cn.hutool.core.util.XmlUtil;
import com.fasterxml.jackson.dataformat.xml.XmlMapper;
import com.gitee.webdav.kernel.Remark;
import lombok.extern.slf4j.Slf4j;

/**
 * @author hongda.li@hand-china.com
 */
@Slf4j
public final class XmlHelper {
    @Remark("XML-Mapper对象")
    private static final XmlMapper MAPPER = new XmlMapper();

    private XmlHelper() {
    }

    public static String toXml(Object obj) {
        try {
            return MAPPER.writeValueAsString(obj);
        } catch (Exception e) {
            log.error("XML序列化失败 : {}", e.getMessage());
            log.error("", e);
            return null;
        }
    }

    public static <T> T fromXml(String xml, Class<T> clazz) {
        try {
            return MAPPER.readValue(xml, clazz);
        } catch (Exception e) {
            log.error("XML反序列化失败 : {}", e.getMessage());
            log.error("{}", XmlUtil.format(xml));
            log.error("", e);
            return null;
        }
    }
}
